<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	Name	: Project class controller
	Author	: Iswan Putera S.Kom
	Date	: 02-11-2013
	Version : 1.0.0
*/

class Karyawan extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('zetro_auth');
		$this->load->model('main_model');
		$this->zetro_auth->check_login();
		//force_ssl();
	}
	function View($filename,$data)
	{
		$this->load->view('admin/header');
		$this->load->view('outsourcing/'.$filename,$data);
		$this->load->view('admin/footer');
	}
	
	function karyawanot()
	{
		$this->zetro_auth->menus('list_karyawan');
		$data['tab_header']=$this->zetro_form->generate_tab_header('Karyawan.');
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		//list tabel
		$content='';
		$this->zetro_form->section('filterkaryawan');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$this->zetro_form->section('karyawanot');
		$this->zetro_form->action_col(true);
		$this->zetro_auth->menus('add_karyawan');
		$this->zetro_form->tabel_content($content);
		$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$data['list']=$this->zetro_form->list_std();
		$data['modal']=$this->zetro_form->dialog('','Add Karyawan','',($this->zetro_auth->cek_oto('c')!='')?true:false,'',(@$_GET['rpt']=='y')?false:false);
		$data['modal2']=$this->zetro_form->PrintPreview('List Karyawan');

		$this->View('project_karyawan',$data);
		
	}
	function detailkaryawan()
	{
		$data=array();
		$data['id_mm']=empty($_POST['id'])?'':$_POST['id'];
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Update Karyawan');
		//generate form basic data
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		$this->zetro_form->section('karyawanfrm');
		$this->zetro_auth->menus('add_karyawan');
		$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi','','id_lokasi');
	    $this->zetro_form->addButtons();
		$this->zetro_form->buttons(false,'Save + Next');
		$data['form22']=$this->zetro_form->frm_std('frm21');
		//generat personal form
		$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$data['jab']  =(@$_GET['ot']=='y')?
						$this->zetro_form->generate_dropdown_content('jabatan_karyawan','id_jab','nm_jabatan'):
						$this->zetro_form->generate_dropdown_content('jabatan','id_jab','nm_jabatan',array('stat_jabatan'=>'Y'));
		$this->zetro_form->section('karyawanfrmdetail');
		$this->zetro_auth->menus('add_karyawan');
		$this->zetro_form->buttons(false,'Save + Next');
		$data['form23']=$this->zetro_form->frm_std('frm31');
		$this->zetro_form->section('karyawangaji');
		$this->zetro_auth->menus('add_karyawan');
		$this->zetro_form->buttons(false,'Saved');
		$data['form24']=$this->zetro_form->frm_std('frm41');
		$data['aksi']=$_POST['aksi'];
		//send data to view
		//$this->load->view('admin/header');
		$this->load->view('outsourcing/project_upd_karyawan',$data);
		//$this->load->view('admin/footer');
	}
	
	function getlistkaryawanot()
	{
		$data=array();$n=0;$content='';
		$where='';
		$ordby=empty($_POST['ordby'])?'nm_karyawan':$_POST['ordby'];
		$this->main_model->table_name('karyawan_list_view');
		$this->main_model->query();
		$this->main_model->where(array('jenis_karyawan'=>($_POST['ot']=='y')?'Outsourcing':'Koperasi'));
		$this->main_model->where(array('stat_karyawan'=>'Y'));
		if(empty($_POST['cari']))
		{
			(empty($_POST['id_lok']))?'':
			$this->main_model->where(array('id_lokasi'=>$_POST['id_lok']));
			(empty($_POST['sch_kary']))?'':
			$this->main_model->where(array('sch_karyawan'=>$_POST['sch_kary']));
		}else{
			$this->main_model->where_like('nm_karyawan',$_POST['cari'],'after');
		}
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_karyawan');
		foreach($lst_usr as $r)
		{
			$n++;
		$icon=($this->zetro_auth->cek_oto('e')!='')?
		$this->zetro_form->z_icon($r->id,array('cus-folder','cus-cross'),array('update','deleted')):"";
		$content.= tr().td($n,'',false,'','center').
				 td($r->nik_karyawan,'',false,'','center').
				 td($r->nm_karyawan).
				 td($r->sex_karyawan,'',false,'','center').
				 td($r->tmp_lahir.", ".tglFromSql($r->tgl_lahir,true)).
				 td($r->hp_karyawan).
				 td($r->nm_lokasi).
				 td($r->nm_jabatan).
				 td(tglFromSql($r->tgl_masuk),'',false,'','center').
				 td($r->sch_karyawan,'',false,'','center').
				 td($icon,'',false,'','center').
				_tr();	
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,10);
		echo $content;
		
	}
	
	function setkaryawanot()
	{
		$data=array();
		$data['id']=empty($_POST['id_karyawan'])?'0':$_POST['id_karyawan'];
		$data['nik_karyawan']=empty($_POST['nik_karyawan'])?'':$_POST['nik_karyawan'];
		$data['nm_karyawan']=empty($_POST['nm_karyawan'])?'':addslashes($_POST['nm_karyawan']);
		$data['sex_karyawan']=empty($_POST['sex_karyawan'])?'':$_POST['sex_karyawan'];
		$data['tmp_lahir']=empty($_POST['tmp_lahir'])?'':addslashes($_POST['tmp_lahir']);
		$data['tgl_lahir']=empty($_POST['tgl_lahir'])?'':tgltoSql($_POST['tgl_lahir']);
		$data['add_karyawan']=empty($_POST['add_karyawan'])?'':addslashes($_POST['add_karyawan']);
		$data['kota_karyawan']=empty($_POST['kota_karyawan'])?'':$_POST['kota_karyawan'];
		$data['prop_karyawan']=empty($_POST['prop_karyawan'])?'':$_POST['prop_karyawan'];
		$data['sch_karyawan']=empty($_POST['sch_karyawan'])?'':$_POST['sch_karyawan'];
		$data['jenis_karyawan']=empty($_POST['jenis_karyawan'])?'Outsourcing':$_POST['jenis_karyawan'];
		$data['jabatan']=empty($_POST['jabatan'])?'':$_POST['jabatan'];
		$data['tgl_masuk']=empty($_POST['tgl_masuk'])?'0000-00-00':tgltoSql($_POST['tgl_masuk']);
		$data['tgl_keluar']=empty($_POST['tgl_keluar'])?'0000-00-00':tgltoSql($_POST['tgl_keluar']);
		$data['id_lokasi']=empty($_POST['id_lokasi'])?'':$_POST['id_lokasi'];
		$data['hp_karyawan']=empty($_POST['hp_karyawan'])?'':$_POST['hp_karyawan'];
		$data['reson_keluar']=empty($_POST['reson_keluar'])?'':addslashes($_POST['reson_keluar']);
		$data['stat_karyawan']=empty($_POST['stat_karyawan'])?'':$_POST['stat_karyawan'];
		$data['created_by']=$this->session->userdata('userid');
		$this->main_model->prepared_data('karyawan',$data);
		echo success($this->main_model->set_data());		
	}
	function setkaryawangaji()
	{
		$data=array();$n=0;$datax=array();
		$this->main_model->table_name('karyawan_gaji_komp_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$datax=$this->main_model->set_data();
		foreach($datax as $r)
		{
			$n++;$jml=0;
			$jml=($r->calc==1)?-(str_replace(",","",$_POST['txt_'.$r->id_komp])):str_replace(",","",$_POST['txt_'.$r->id_komp]);
			$data['id_karyawan']=$_POST['id_karyawan'];
			$data['thn_karyawan']=empty($_POST['thn_karyawan'])?date('Y'):$_POST['thn_karyawan'];
			$data['jenis_gaji']=$r->id_komp;
			$data['jumlah']=empty($_POST['txt_'.$r->id_komp])?'0':$jml;
			
			$data['calc']=$r->calc;
			$this->main_model->prepared_data('karyawan_gaji',$data);
			echo success($this->main_model->set_data());
		}
		
	}
	function getdetailot()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('karyawan');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);
		
	}
	function getdetailgaji()
	{
		$data=array();$content='';$datax=array();$thn="";
		$id=$_POST['id'];
		$thn_ini=$this->main_model->rdb('karyawan_gaji_thn',"thn_karyawan"," order by thn_karyawan desc limit 1");
		$thn=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan');
		$thn.=($thn_ini=='')?"<option value='".date('Y')."' selected'>".date('Y')."</option>":"";
		$content="<form id='frm41' name='frm41' action='' method='post' class='form-horizontal'>";
		$content.="<div class='control-group'><label class='control-label' for='thn_gaji'>Tahun</label>
					<div class='controls'><input type='text' id='thn_gaji' name='thn_gaji' class='span2' value='".date('Y')."' readonly></div>
					<div class='controls'></div></div><hr>";
		$this->main_model->table_name('karyawan_gaji_grp');
		$this->main_model->query();
		$this->main_model->set_methods('select');
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$content.="<div class='alert' style='width:90%'><b>".$r->nm_group."</b></div>";
			$ordby=empty($_POST['ordby'])?'id_komp':$_POST['ordby'];
			$this->main_model->table_name('karyawan_gaji_komp');
			$this->main_model->where(array('id_grp_komp'=>$r->id_group));
			$this->main_model->orderby($ordby);			
			$this->main_model->query();
			$this->main_model->set_methods('select_ordered');
			$datax=$this->main_model->set_data();
			foreach($datax as $rr)
			{
				$val_gaji=0;
				$val_gaji=$this->main_model->rdb('karyawan_gaji',"jumlah","where id_karyawan='".$id."' and thn_karyawan='".date('Y').
												 "' and jenis_gaji='".$rr->id_komp."'");
				$content.="<div class='control-group' id='g_".$rr->id_komp."'>";
				$content.="<label class='control-label' for='txt_".$rr->id_komp."'>".$rr->nm_komp."</label>";
				$content.="<div class='controls' style='width:50%'>";
				$content.="<input type='text' id='txt_".$rr->id_komp."' name='txt_".$rr->id_komp.
						  "' class='span2 angka' value='".$val_gaji."'/>";
				$content.="</div><div class='controls'></div></div>";	
			}
		}
		$content.="</form>";
		echo $content;
	}
	function getvaluegaji()
	{
		$data=array();
		$id=$_POST['id'];
		$thn=$_POST['thn'];
			$this->main_model->table_name('karyawan_gaji');
			$this->main_model->where(array('id_karyawan'=>$id,'thn_karyawan'=>$thn));
			$this->main_model->query();
			$this->main_model->set_methods('select');
			$data=$this->main_model->set_data();
			echo ($data)?json_encode($data):json_encode(array('txt_1'=>'0'));
	}
	function deletekaryawanot()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('karyawan');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
		
	}
	
	//komposisi gaji
	function payrollkomp()
	{
		$this->zetro_auth->menus('komposisi_gaji');
		$data['tab_header']=$this->zetro_form->generate_tab_header('Komposisi Gaji');
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		$content='';
		$this->zetro_form->section('KompGaji');
		$this->zetro_form->action_col(true);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','70%');
		$this->zetro_auth->menus('komposisi_gaji');
		$this->zetro_form->section('KompGajifrm');
		//$this->zetro_form->addButtons();
		$this->zetro_form->buttons(true,'Saved');
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm1'),'Add Komponen');
		$data['test']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_grp','id_group','nm_group','','id_group');		
		$data['id']=empty($_POST['id'])?'0':$_POST['id'];
		
		$this->View('project_paykomp',$data);
	}
	
	function getpayrollkomp()
	{
		$content='';$n=0;
		$ordby=empty($_POST['ordby'])?'id_group':$_POST['ordby'];
		$this->main_model->table_name('karyawan_gaji_grp');
		$this->main_model->query();
		$this->main_model->orderby($ordby);
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('komposisi_gaji');
		foreach($lst_usr as $r)
		{
			$n++;$nn=0;
			//komponen gaji
			$ordby=empty($_POST['ordby'])?'id_komp':$_POST['ordby'];
			$this->main_model->table_name('karyawan_gaji_komp_v');
			$this->main_model->query();
			$this->main_model->orderby($ordby);
			$this->main_model->where(array('id_grp_komp'=>$r->id_group));
			$this->main_model->set_methods('select_ordered');
			$lst_usr1=$this->main_model->set_data();


			$icon=($this->zetro_auth->cek_oto('e')!='')?
			$this->zetro_form->z_icon($r->id_group,array('cus-add','cus-'),array('addnew','')):"";
			$content.= tr().td($n,'',false,'','center').
					   td($r->nm_group).
					   td($icon,'',false,'','center').
					  _tr();
				foreach($lst_usr1 as $rr)
				{
					$nn++;
					$icon=($this->zetro_auth->cek_oto('e')!='')?
					$this->zetro_form->z_icon($rr->id_komp,array('cus-folder','cus-cross'),array('update','deleted')):"";
					$content.= tr().td('','',false,'','center').
							   td(nbs(5)."<span class='span3'>".$nn.". ". $rr->nm_komp."</span><span class='span2 angka'>".
							   number_format($rr->val_stdkomp,0)."</span>").
							   td($icon,'',false,'','center').
							  _tr();
				}
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,3);
		echo $content;
		
	}
	
	function detailpayrollkomp()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('karyawan_gaji_komp');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_komp'=>$id));
		$data=$this->main_model->set_data();
		echo ($data)?json_encode($data[0]):json_encode(array('id_komp',''));
	}
	
	function setpayrollkomp()
	{
		$data=array();
		$data['id_komp']=empty($_POST['id_komp'])?'0':$_POST['id_komp'];
		$data['id_grp_komp']=empty($_POST['id_group'])?'':$_POST['id_group'];
		$data['nm_komp']=empty($_POST['nm_komp'])?'':$_POST['nm_komp'];
		$data['val_stdkomp']=empty($_POST['val_stdkomp'])?'0':str_replace(",","",$_POST['val_stdkomp']);
		$data['stat_komp']=empty($_POST['stat_komp'])?'Y':$_POST['stat_komp'];
		$data['created_by']=$this->session->userdata('userid');
		$this->main_model->prepared_data('karyawan_gaji_komp',$data);
		echo success($this->main_model->set_data());		
	}
	
	function deletepayrollkomp()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('karyawan_gaji_komp');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_komp'=>$id));
		$this->main_model->set_data();	
	}
	//slip gaji
	function payroll()
	{
		$this->zetro_auth->menus('daftar_gaji');
		$data['tab_header']=$this->zetro_form->generate_tab_header('Slip Gaji');
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		//list tabel
		$content='';
		$this->zetro_form->section('slipfrm');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$this->zetro_form->section('slipgaji');
		$this->zetro_form->action_col(true);
		$this->zetro_auth->menus('daftar_gaji');
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$data['bln']=dropdownBln(date('m'));
		$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		$data['modal']=$this->zetro_form->dialog('','Update Salary','');		
		$this->View('project_payslip',$data);
	}
	
	function getdatagajian()
	{
		$data=array();$n=0;$content='';
		$where='';
		$bln=empty($_POST['bln'])?date('m'):$_POST['bln'];
		$thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$ordby=empty($_POST['ordby'])?'nm_karyawan':$_POST['ordby'];
		$this->main_model->table_name('karyawan_list_view');
		$this->main_model->query();
		$this->main_model->where(array('jenis_karyawan'=>($_POST['ot']=='y')?'Outsourcing':'Koperasi'));
		$this->main_model->where(array('stat_karyawan'=>'Y'));
		if(empty($_POST['cari']))
		{
			(empty($_POST['id_lok']))?'':
			$this->main_model->where(array('id_lokasi'=>$_POST['id_lok']));
			(empty($_POST['sch_kary']))?'':
			$this->main_model->where(array('sch_karyawan'=>$_POST['sch_kary']));
		}else{
			$this->main_model->where_like('nm_karyawan',$_POST['cari'],'after');
		}
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_karyawan');
		foreach($lst_usr as $r)
		{
			$n++;
		$icon=($this->zetro_auth->cek_oto('e')!='' && empty($_POST['l']))?
		$this->zetro_form->z_icon($r->id,array('cus-folder','cus-cross'),array('update','deleted')):"";
		$content.= tr().td($n,'',false,'','center').
				 td($r->nik_karyawan,'',false,'','center').
				 td($r->nm_karyawan).
				 td($r->nm_jabatan).
				 td($this->main_model->rdb("employee_absensi_tjam","jamkerja","where nik_employee='".$r->id."' and bln='".$bln."' and thn='".$thn."'"),'',false,'','center').
				 td($this->main_model->rdb("employee_absensi_tjam","jamlembur","where nik_employee='".$r->id."' and bln='".$bln."' and thn='".$thn."'"),'',false,'','center').
				 td(number_format($this->main_model->rdb("karyawan_gajian","(sum(salari)+sum(lembur)) as salari","where nik_employee='".$r->id."' and bln='".$bln."' and thn='".$thn."'"),2),'',false,'','right').
				 td($icon,'',false,'','center').
				_tr();	
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,10);
		echo $content;
		
		
	}
	//update gajian
	function updatedatagaji()
	{
		$data=array();$content='';$datax=array();$thn="";
		$id=$_POST['id'];
		$thn_ini=$this->main_model->rdb('karyawan_gaji_thn',"thn_karyawan"," order by thn_karyawan desc limit 1");
		$thn=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan');
		$thn.=($thn_ini=='')?"<option value='".date('Y')."' selected'>".date('Y')."</option>":"";
		$content="<form id='frm41' name='frm41' action='' method='post' class='form-horizontal'>";
		$content.="<div class='control-group'><label class='control-label' for='thn_gaji'>Tahun</label>
					<div class='controls'><input type='text' id='thn_gaji' name='thn_gaji' class='span2' value='".date('Y')."' readonly></div>
					<div class='controls'></div></div><hr>";
		$this->main_model->table_name('karyawan_gaji_grp');
		$this->main_model->query();
		$this->main_model->set_methods('select');
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$content.="<div class='alert' style='width:90%'><b>".$r->nm_group."</b></div>";
			$ordby=empty($_POST['ordby'])?'id_komp':$_POST['ordby'];
			$this->main_model->table_name('karyawan_gaji_komp');
			$this->main_model->where(array('id_grp_komp'=>$r->id_group));
			$this->main_model->orderby($ordby);			
			$this->main_model->query();
			$this->main_model->set_methods('select_ordered');
			$datax=$this->main_model->set_data();
			foreach($datax as $rr)
			{
				$val_gaji=0;
				$val_gaji=$this->main_model->rdb('karyawan_gaji',"jumlah","where id_karyawan='".$id."' and thn_karyawan='".date('Y').
												 "' and jenis_gaji='".$rr->id_komp."'");
				$content.="<div class='control-group' id='g_".$rr->id_komp."'>";
				$content.="<label class='control-label' for='txt_".$rr->id_komp."'>".$rr->nm_komp."</label>";
				$content.="<div class='controls' style='width:50%'>";
				$content.="<input type='text' id='txt_".$rr->id_komp."' name='txt_".$rr->id_komp.
						  "' class='span2 angka' value='".$val_gaji."'/>";
				$content.="</div><div class='controls'></div></div>";	
			}
		}
		$content.="</form>";
		echo $content;
	}
	//absensi
	function absensi()
	{
		$this->zetro_auth->menus('absensi');
		$data['tab_header']=$this->zetro_form->generate_tab_header('Absensi Harian');
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		//list tabel
		$content='';
		$this->zetro_form->section('frmabsen');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$this->zetro_form->section('listabsen');
		$this->zetro_form->action_col(false);
		$this->zetro_auth->menus('daftar_gaji');
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','80%');
		
		$this->zetro_form->section('popAbsen');
		$this->zetro_auth->menus('absensi');
		$this->zetro_form->buttons(true,'Saved');
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm4'),'Absen','');		
		$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$data['bln']=dropdownBln(date('m'));
		$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		$this->View('project_absensi',$data);
		
	}
	function listabsens()
	{
		$data=array();$n=0;
		$thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$bln=empty($_POST['bln'])?date('m'):str_repeat('0',(2-strlen($_POST['bln']))).$_POST['bln'];
		$nik=empty($_POST['id_karyawan'])?'-':$_POST['id_karyawan'];
		$nik=explode("-",$nik);
		$content="";
		$jml_hari=cal_days_in_month(CAL_GREGORIAN,$bln, $thn);		
		$this->zetro_auth->menus('daftar_gaji');
		$ijin=$this->zetro_auth->cek_oto('c');
		for($i=1;$i<=$jml_hari;$i++)
		{
			$checked='';$c_a='';$c_c='';$c_h='';$ttl='';$stat_ot='';$ttlo='';$ot='00:00:00';
			$hari=date('w',strtotime($thn.'/'.$bln.'/'.str_repeat('0',(2-strlen($i))).$i));
			$today=($thn.$bln.$i==date('Ymd'))?'alert-info':'xx';
			$checked=$this->main_model->rdb('employee_absensi','status_absen',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'");
			$ID=$this->main_model->rdb('employee_absensi','ID',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'");
			$ot=$this->main_model->rdb('employee_absensi_time','jam_ot',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'");
			$ttl=$this->main_model->rdb('employee_absensi','jam_masuk',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'");
			$c_h=($checked=='H')?"checked='checked' title='".$ttl."'":'';
			$c_c=($checked=='C')?"checked='checked' title='".$ttlo."'":'';
			$c_a=($checked=='A')?"checked='checked' title='".$ttlo."'":'';
			$nonaktif=($ijin=='' || $nik[1]=='')?"disabled='disabled'":'';
			$libur=($hari==0)?'alert':'';
			$tat_ot=$this->main_model->rdb('employee_absensi','stat_lembur',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'");
			$ck_box=($tat_ot=='Y')?"checked='checked'":'';
			$content.=tr($libur.' '.$today).td("<span>$i<span>",'',false,'','center').
					  td(nHari($hari),'',false,$libur,'left').
				 	  td(str_repeat('0',(2-strlen($i))).$i,'',false,'','center').
					  td("<input type='radio' $nonaktif class='h_".$i."' value='H' name='masuk_".$i."' id='masuk_".$i."' ".$c_h." style='cursor:pointer' onclick=\"absen('H','".str_repeat('0',(2-strlen($i))).$i."/".$bln."/".$thn."','".$nik[1]."','".$i."')\"/>",'',false,'','center').
					  td("<input type='radio' $nonaktif class='c_".$i."' value='C' name='masuk_".$i."' id='masuk_".$i."' ".$c_c." style='cursor:pointer' onclick=\"absen('C','".str_repeat('0',(2-strlen($i))).$i."/".$bln."/".$thn."','".$nik[1]."','".$i."')\"/>",'',false,'','center').
					  td("<input type='radio' $nonaktif class='a_".$i."' value='A' name='masuk_".$i."' id='masuk_".$i."' ".$c_a." style='cursor:pointer' onclick=\"absen('A','".str_repeat('0',(2-strlen($i))).$i."/".$bln."/".$thn."','".$nik[1]."','".$i."')\"/>",'',false,'','center').
					  td(($checked=='H')?$this->main_model->rdb('employee_absensi_time','jam',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn.$bln.str_repeat('0',(2-strlen($i))).$i."'"):'','',false,'','center',"kotak jam_".$i).
					  td(($tat_ot=='Y' ||$ot!='00:00:00' && $ot!='' )?"<input style='display:blok' type='checkbox' $nonaktif name='l_".$ID."' id='l_".$ID."' $ck_box />":"<input style='display:none' class='ck_".$i."' type='checkbox' $nonaktif name='l_".$ID."' id='l_".$ID."' $ck_box />",'',
					  false,"ot_".$i,'center').
					  td(($ot=='00:00:00')?'':$ot,'',false,'kotak tot_'.$i,'center').
					  td(($stat_ot=='Y' || $ot!='00:00:00' && $checked=='H' )?
					  $this->main_model->rdb('employee_absensi','ket_lembur',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'"):
					  $this->main_model->rdb('employee_absensi','ket_absen',"where nik_employee='".$nik[1]."' and tgl_absen='".$thn."-".$bln."-".str_repeat('0',(2-strlen($i))).$i."'")).
					  td(($checked!="")?"<i class='cus-delete' id='d_$i' onclick=\"btn_click('".$ID."','del')\"><i>":"",'1%',false,'','center').
					 _tr();	
		}
		echo $content;
	}
	
	function setabsensi()
	{
		$data=array();
		$data['jam_masuk']=empty($_POST['jam_masuk'])?'00:00':$_POST['jam_masuk'];
		$data['jam_keluar']=empty($_POST['jam_keluar'])?'00:00':$_POST['jam_keluar'];
		$data['status_absen']=empty($_POST['stat'])?'a':$_POST['stat'];
		$data['nik_employee']=empty($_POST['nik_employee'])?'':$_POST['nik_employee'];
		$data['tgl_absen']=empty($_POST['tgl_absen'])?date('Ymd'):tgltoSql($_POST['tgl_absen']);
		$data['ket_absen']=empty($_POST['ket_absen'])?'':$_POST['ket_absen'];
		$data['created_by']=$this->session->userdata('userid');
		$this->main_model->prepared_data('employee_absensi',$data);
		echo success($this->main_model->set_data());		
	}
	function settidaklembur()
	{
		$id=$_POST['id'];
		$ket_lembur=empty($_POST['reson'])?'Tidak dianggap lembur by '.$this->session->userdata('username'):$_POST['reason'];
		$this->main_model->get_content(array('ket_lembur'=>$ket_lembur,'stat_lembur'=>'N'));
		$this->main_model->where(array('ID'=>$id));
		$this->main_model->update_data('employee_absensi');
	}
	
	function setlembur()
	{
		$id=$_POST['id'];
		$ket_lembur=empty($_POST['reson'])?'':$_POST['reson'];
		$this->main_model->get_content(array('ket_lembur'=>$ket_lembur,'stat_lembur'=>'Y'));
		$this->main_model->where(array('ID'=>$id));
		$this->main_model->update_data('employee_absensi');
	}
	function delabsensiharian()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('employee_absensi');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
	}
    function gettotaljam()
	{
		$nik=$_POST['nik'];
		$tgl=$_POST['tgl'];
		$jam=$this->main_model->rdb('employee_absensi_time','jam',"where nik_employee='".$nik."' and tgl_absen='".$tgl."'");
		$lbr=$this->main_model->rdb('employee_absensi_time','jam_ot',"where nik_employee='".$nik."' and tgl_absen='".$tgl."'");	
		echo $jam."|".$lbr;
	}
    function gettotaljambulan()
	{
	/** 
		get total jam per bulan
	*/
		$jamkerja='00:00:00';$jamlembur='00:00:00';
		$thn=empty($_POST['tahun'])?date('Y'):$_POST['tahun'];
		$bln=empty($_POST['bln'])?date('m'):str_repeat('0',(2-strlen($_POST['bln']))).$_POST['bln'];
		$nik=empty($_POST['id_karyawan'])?'-':$_POST['id_karyawan'];
		$nik=explode("-",$nik);
		$jamkerja= $this->main_model->rdb('employee_absensi_tjam','jamkerja',"where nik_employee='".$nik[1]."' and Bln='".$bln."' and Thn='".$thn."'");	
		$jamlembur= $this->main_model->rdb('employee_absensi_tjam','jamlembur',"where nik_employee='".$nik[1]."' and Bln='".$bln."' and Thn='".$thn."'");	
		echo $jamkerja."|".$jamlembur;
	}
	function getemployeename()
	{
		/**
		* populate daftar karyawan untuk proses auto complete typeahead
		*/
		$html="";
		$this->main_model->table_name('karyawan');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('stat_karyawan'=>'Y'));
		$this->main_model->where(($_POST['ot']=='y')?array('jenis_karyawan'=>'Outsourcing'):array('jenis_karyawan'=>'Koperasi'));
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$html[]=strtoupper(str_replace("-","_",$r->nm_karyawan))."-".$r->id;
		}
		echo json_encode($html);
	}
	function list_absensi()
	{
		$this->zetro_auth->menus('absensi');
		$data['tab_header']=$this->zetro_form->generate_tab_header('Data Absensi');
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		//list tabel
		$content='';
		$this->zetro_form->section('frmabsensi');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$this->zetro_form->section('listabsensi');
		$this->zetro_form->action_col(false);
		$this->zetro_auth->menus('daftar_gaji');
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','100%');
		$this->zetro_form->section('popAbsen');
		$this->zetro_auth->menus('absensi');
		$this->zetro_form->buttons(true,'Saved');
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm4'),'Absen','');
		$data['modal2']=$this->zetro_form->PrintPreview('List Absensi');		
		$data['lok2'] =$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		$data['bln']=dropdownBln(date('m'));
		$data['thn']=$this->zetro_form->generate_dropdown_content('karyawan_gaji_thn','thn_karyawan','thn_karyawan','',date('Y'));
		$this->View('project_absensi_list',$data);
	}
	function getlistabsensi()
	{
		$data=array();$n=0;
		$bln=empty($_POST['bln'])?date('m'):$_POST['bln'];
		$thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$this->main_model->table_name('employee_absensi_report');
		if(empty($_POST['id_karyawan']))
		{
			$cari='';
			$this->main_model->where(array('bln'=>$bln,'thn'=>$thn));
			empty($_POST['id_lokasi'])?'':
			$this->main_model->where(array('id_lokasi'=>$_POST['id_lokasi']));
		}else
		{
			$cari='y';
			$this->main_model->where(array('thn'=>$thn));
			$this->main_model->where_like('nm_employee',$_POST['id_karyawan'],'after');
		}
		$this->main_model->where(($_POST['ot']=='y')?array('jenis_karyawan'=>'Outsourcing'):array('jenis_karyawan'=>'Koperasi'));
		$this->main_model->query();
		$this->main_model->set_methods('select');
		$data=$this->main_model->set_data();
		echo tr('alert alert-info').td().td().td();
		for ($x=1;$x<=31;$x++)
		{
			echo td("Kerja",'4%',false,'','Center').td("OT",'4%',false,'','Center');
		}
		echo _tr();
		foreach($data as $r)
		{
			$n++;
			echo tr().td(($cari=='')?$n:$r->bln,'',false,'','center').
				 td($r->nik_employee,'',false,'','center').
				 td(strtoupper($r->nm_employee));
				 for($i=1;$i<=31;$i++)
				 {
					 $nam="d_$i";$ot="t_$i";
					echo td($r->$nam,'',false,'','center').
						 td($r->$ot,'',false,'','center'); 
				 }
				 
			echo  _tr();
		}
		echo $this->zetro_form->DataNotFound($data,34);
	}
	function printabsenbulanan()
	{
		$content="";
		$data=array();$n=0;
		$this->zetro_form->frm_file('asset/frm/zetro_project.frm');
		$this->zetro_form->section('listabsensi');
		$this->zetro_form->action_col(false);
		$this->zetro_auth->menus('daftar_gaji');
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','100%');
		
		$bln=empty($_POST['bln'])?date('m'):$_POST['bln'];
		$thn=empty($_POST['thn'])?date('Y'):$_POST['thn'];
		$this->main_model->table_name('employee_absensi_report');
		if(empty($_POST['id_karyawan']))
		{
			$cari='';
			$this->main_model->where(array('bln'=>$bln,'thn'=>$thn));
			empty($_POST['id_lokasi'])?'':
			$this->main_model->where(array('id_lokasi'=>$_POST['id_lokasi']));
		}else
		{
			$cari='y';
			$this->main_model->where(array('thn'=>$thn));
			$this->main_model->where_like('nm_employee',$_POST['id_karyawan'],'after');
		}
		$this->main_model->where(($_POST['ot']=='y')?array('jenis_karyawan'=>'Outsourcing'):array('jenis_karyawan'=>'Koperasi'));
		$this->main_model->query();
		$this->main_model->set_methods('select');
		$data['cari']=$cari;
		$data['bln']=$bln;
		$data['thn']=$thn;
		$data['id_lokasi']=empty($_POST['id_lokasi'])?'All':$this->main_model->rdb('lokasi','nm_lokasi',"where id_lokasi='".$_POST['id_lokasi']."'");
		$data['lst_usr']=$this->main_model->set_data();
		//generate PDF
		$this->load->library('zetro_dompdf');
		$html = $this->load->view('outsourcing/project_absensi_list_print', $data, true);
		$this->zetro_dompdf->createPDF($html, $this->session->userdata('userid').'_project_absensi_list', true,'landscape','A3');
		echo base_url().'application/logs/'.$this->session->userdata('userid').'_project_absensi_list.pdf';/**/
	}
	//end of karyawan class
}